package com.gemptc.service.iphone;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.gemptc.bean.Sciencebean;
import com.gemptc.daoimp.SelectScienceImp;
//查询科普推荐表的信息
/**
 * Servlet implementation class ScienceService
 */
@WebServlet("/ScienceService")
public class ScienceService extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ScienceService() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		int  c;
		PrintWriter printWriter = response.getWriter();
		System.out.println("信息");
		String Value = request.getParameter("key");
		switch (Integer.parseInt(Value)) {
		case 1:
			// 定义一个list集合 里面是sciencebean；
			List<Sciencebean> list = new ArrayList<Sciencebean>();
			SelectScienceImp scienceImp = new SelectScienceImp();
			// 设置索引
			String photo = request.getParameter("page");
			// 通过selectImp类里的getSciencebean方法获取photo
			list = scienceImp.getSciencebean(Integer.parseInt(photo));
			// 使用jsonArray解析selectImp里的getSciencebean方法查询到的数据库的信息
			JSONArray jsonArray = JSONArray.fromObject(list);
			// 传换成一个字符串类型；
			printWriter.write(jsonArray.toString());
			printWriter.close();
			break;
		case 2:
			// 定义一个list集合 里面是sciencebean；
			List<Sciencebean> list2 = new ArrayList<Sciencebean>();
			SelectScienceImp scienceImp1 = new SelectScienceImp();
			// 设置索引
			String photo2 = request.getParameter("page");
			// 通过selectImp类里的getSciencebean方法获取photo
			list2 = scienceImp1.getSciencebeanKePu(Integer.parseInt(photo2));
			// 使用jsonArray解析selectImp里的getSciencebean方法查询到的数据库的信息
			JSONArray jsonArray2 = JSONArray.fromObject(list2);
			// 传换成一个字符串类型；
			printWriter.write(jsonArray2.toString());
			printWriter.close();
			break;
		case 3:
			String user_id1 = request.getParameter("user_id");
			String s_id1 = request.getParameter("s_id");
		    c=Integer.parseInt(request.getParameter("c"));
			SelectScienceImp scienceImp2 = new SelectScienceImp();
			int s_id2 = Integer.parseInt(s_id1);
			int user_id2 = Integer.parseInt(user_id1);
			boolean flag = scienceImp2.Collect(user_id2, s_id2,c);
			System.out.println("flag-------"+flag);
			if (flag) {
				System.out.println("收藏");
				printWriter.write("已收藏");
			} else {
				System.out.println("weishoucang");
				printWriter.write("没有收藏");
			}
			printWriter.close();
			break;
		case 4:
			System.out.println(7);
			String user_id3 = request.getParameter("user_id");
			String s_id3 = request.getParameter("s_id");
			 c=Integer.parseInt(request.getParameter("c"));
			SelectScienceImp scienceImp3 = new SelectScienceImp();
			int s_id4 = Integer.parseInt(s_id3);
			int user_id4 = Integer.parseInt(user_id3);
			boolean flag1 = scienceImp3.Collect(user_id4, s_id4,c);
			System.out.println(flag1);
			if (flag1) {
				printWriter.write("收藏成功");
			} else {
				printWriter.write("收藏失败");
			}
			printWriter.close();
			break;
		case 5:
			System.out.println(8);
			String user_id5 = request.getParameter("user_id");
			String s_id5 = request.getParameter("s_id");
		    c=Integer.parseInt(request.getParameter("c"));
			SelectScienceImp scienceImp4 = new SelectScienceImp();
			int s_id6 = Integer.parseInt(s_id5);
			int user_id6 = Integer.parseInt(user_id5);
			boolean flag2 = scienceImp4.Collect(user_id6, s_id6 ,c);
			System.out.println(flag2);
			if (flag2) {
				printWriter.write("已取消收藏");
			} else {
				printWriter.write("取消收藏失败");
			} 
			printWriter.close();
			break;
		default:
			break;
	
		}
	}

}
